<include file="../header" />
<body>
	<div class="wraper">
		<div class="load">
			<img class="load-bee" src="/assets/repairnew/images/load-bee.png"/>
		</div>
	    <!-- <div class="top">
	        <p><a href="javascript:history.back(-1)" class="iconfont icon-zuo fl-left"></a></p>
            <p class="search-box pull-left"><input placeholder="请输入物料关键字"/><span class="iconfont icon-fangda"></span></p>
	        <p><a href="#" class="fl-right">搜索</a></p>
	    </div> -->
    <div>

    <div class="clearfix">
        <p class="form-box half-form fl-left">
            <select class="select" id="material_type">
                <option value="0">请选择物料一级分类</option>
                <volist name="materialTypeList" id="vo">
                    <option value="{BEESCRM:$vo.material_type_id}">
                        {BEESCRM:$vo['material_type_name']}
                    </option>
                </volist>
            </select>
            <span class="triangle"></span>
        </p>
        <p class="form-box half-form fl-left" id="child_type">
            <select class="select" id="material_child_type" >
                <option value="0">请选择物料二级分类</option>
            </select>
            <span class="triangle"></span>
        </p>
    </div>

    <div class="clearfix" id="materialData"></div>
    <div class="marginb-50"></div>
    <div class="btn-box">
        <p class="percent30 fl-left bg-white text-16 totalNum"><span class="iconfont icon-gaojigongju"></span><font>0</font></p>
        <a href="javascript:void(0)" class="btn percent70 addorder fl-left">确认申领</a>
    </div>
</div>



<include file="../commonJS" />
<script type="text/javascript" src="/assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.fileupload.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.iframe-transport.js"></script>
<script type="text/javascript">
	$(function(){
		$(document).on('change','#material_type',function(){
            $('.totalNum font').text(0);
			var materialType = $(this).val();
			if(materialType == 0){
//				$('#child_type').hide();
                $('#materialData').html('<div class="block-space"><p>暂无物料</p></div>');
				return false;
			}

			$.ajax({
				url:"/repairnew/repairnewMaterial/getSecond/repairId/"+"{BEESCRM:$repairId}",
				type:"post",
				data:{'material_type':materialType},
				dataType:'json',
				success:function(res){
					var str = '';
					if(res.data){
						var arr = res.data;
                        str+= '<option value="0">请选择物料二级分类</option>';
						for(var i=0;i<arr.length;i++){
							str+="<option value='"+arr[i].material_type_id+"'>"+arr[i].material_type_name+"</option>";
//							$('#child_type').show();
						}
					}else{
						str = '<option value="0">请选择物料二级分类</option>';
//						$('#child_type').hide();
					}

					$('#material_child_type').html(str);
                    if(res.materialList){
                        materialData(res.materialList);
                    }else{
                        $('#materialData').html('<div class="block-space"><p>暂无物料</p></div>');
                    }
				}
			})
		})
        $(document).on('change','#material_child_type',function(){
            var materialType = $(this).val();
            $.ajax({
                url:"/repairnew/repairnewMaterial/getMaterialBySecondType/repairId/"+"{BEESCRM:$repairId}",
                'type':'post',
                data:{'material_type':materialType},
                dataType:"json",
                success:function(res){
                    if(res.data){
                        materialData(res.data);
                    }
                }
            })
        }) 


        function materialData(data){
            var str = '';
            if(data.length>0){
                for(i=0;i<data.length;i++){
                    str+='<div class="list tb-mode">'
                            +'<div class="tb-cell lineheight15">'
                                +'<p>'+data[i].material_name+'</p>'
                            +'</div>'

                    +'<div class="tb-cell material_num">'
                        +'<div class="quantity pull-right" data-stock="'+data[i].stock+'">'
                            +'<span class="change-num-btn btn_minus fl-left "  data-material_id="'+data[i].material_id+'">-</span>'
                            +'<input type="number" value="0" readonly="readonly" class="buy-count fl-left "/>'
                            +'<span class="change-num-btn btn_plus fl-left "  data-material_id="'+data[i].material_id+'">+</span>'
                        +'</div>'
                     +'</div>'+'</div>'
                }
            }else{
                str='<div class="block-space">' +'<p>暂无物料</p>' +'</div>';
            }

            $('#materialData').html(str);
        }   

        $(document).ready(function(){
            $(document).on('click','.btn_minus',function(){
                var value = parseInt($(this).next().val());
                if(value <= 1){
                    value = 0;
                    //$(this).addClass('hide_right').siblings().addClass('hide_right');
                }else{
                    value--;
                }
                
                $(this).next().val(value);
                $('.totalNum font').text(getTotalNum());
            });

            $(document).on('click','.btn_plus',function(){
                var value = parseInt($(this).prev().val());
                if(value <= 0){
                    value = 1;
                    //$(this).removeClass('hide_right').siblings().removeClass('hide_right');
                }else{
                    value++;
                }

                $(this).prev().val(value);
                $('.totalNum font').text(getTotalNum());
            });
        });

        function getTotalNum(){
           var totalNum = 0;//总数
           $('.material_num').each(function(){
                num = parseInt($(this).find('.buy-count').val());
                if(num>0){
                    totalNum+=num;
                }
           })
           return totalNum;
        }


        //物料申请提交
        $(document).on('click','.addorder',function(){
            var data = {};
            var material_type = $('#material_type').val();
            if(material_type == 0){
                $.alert('error','请选择一级分类');return false;
            }
            data.material_type = material_type;

           var material_child_type = $('#material_child_type').val();
           if(material_child_type == 0){
                $.alert('error','请选择二级分类');return false;
            }
           data.material_child_type = material_child_type;

           var submit_material = [];
           var num=0;//值
           var materialId = '';
           var totalNum = 0;//总数
           $('.material_num').each(function(){
                num = parseInt($(this).find('.buy-count').val());
                if(num>0){
                    materialId = $(this).find('.btn_minus').data('material_id');
                    submit_material.push(materialId+'-'+num);
                    totalNum+=num;
                }
           })
           
           if(totalNum==0){
             $.alert('error','至少选择一件物品');return false;
           }

           
           data.submit_material = submit_material.join(',');
           data.order_id = "{BEESCRM:$orderId}";

           $.ajax({
             url:"/repairnew/repairnewMaterial/apply/repairId/"+"{BEESCRM:$repairId}",
             type:"post",
             data:data,
             dataType:'json',
             success:function(res){
                if(res.status == 'S'){
                    $.alert('success',res.message);
                    setTimeout('location.href="/index.php?g=repairnew&m=repairnewReceive&a=index&repairId={BEESCRM:$repairId}&order_id={BEESCRM:$orderId}"',2000);
                }else{
                    $.alert('error',res.message);
                }
             }
           })


        })


	})
</script>
</body>
</html>
